package com.yuanqiao.interfaceforlc.uploadmatterandfile.util;

import java.io.*;

import com.aspose.words.*;
public class wordtopdfUtil {
    //校验license
    private static boolean judgeLicense() {
        boolean result = false;
        try {
            InputStream is = wordtopdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    // 转换
    public static void trans(String filePath, String pdfPath) throws Exception {
        if (!judgeLicense()) {
            System.out.println("license错误");
        }
        try {
            System.out.println("as开始：" + filePath);
            long old = System.currentTimeMillis();
            File file = new File(pdfPath);
            toPdf(file, filePath);
            long now = System.currentTimeMillis();
            System.out.println("完成：" + pdfPath);
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private static void toPdf(File file, String filePath) throws Exception {

            wordofpdf(file, filePath);
    }

    private static void wordofpdf(File file, String filePath) throws Exception {
        FileOutputStream os = null;
        Document doc;
        try {
            os = new FileOutputStream(file);
            doc = new Document(filePath);
            doc.save(os, com.aspose.words.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
